package com.vil.controller;

import com.vil.common.Constants;
import com.vil.common.ServerResponse;
import com.vil.entity.Admin;
import com.vil.entity.Equipment;
import com.vil.entity.Village;
import com.vil.service.EquipmentService;
import com.vil.service.VillageService;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("asset")
public class EquipmentController extends BaseController{

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private VillageService villageService;

    //去设备资产页面
    @RequestMapping("assetEquipmentList.html")
    public String assetEquipmentList(Model model, HttpSession session){
        Admin currentAdmin = (Admin)session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
            return "admin/login";
        }
        ServerResponse serverResponse = equipmentService.getListByManyConditions(1, 5, null);
        model.addAttribute("eq",serverResponse.getData());
        model.addAttribute("admin",currentAdmin);
        return "admin/device_list";
    }

    //获取资产列表
    @PostMapping("viewequipmentlist")
    @ResponseBody
    public ServerResponse viewequipmentlist(@RequestParam(value = "condition",defaultValue = "",required = false) String condition,
                                            @RequestParam(value = "condition1",defaultValue = "",required = false) String condition1,
                                            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                                            @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize,
                                            Model model){
        Equipment equipment = new Equipment();
        equipment.setMark(condition);
        equipment.setEquipment_name(condition1);
        return equipmentService.getListByManyConditions(pageIndex,pageSize,equipment);
    }

    //去新增页面
    @GetMapping("device_add.html")
    public String device_add(Model model){
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/device_add";
    }

    //新增设备
    @PostMapping("addDeviceSelective")
    public ServerResponse addDeviceSelective(Equipment equipment){
       return equipmentService.insertSelective(equipment);
    }

    //去编辑页面
    @GetMapping("device_edit.html/{id}")

    public String device_edit(@PathVariable Long id,Model model){
        ServerResponse byId = equipmentService.getById(id);
        model.addAttribute("e",byId.getData());
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/device_edit";
    }

    //修改
    @PostMapping("editDevice")
    @ResponseBody
    public ServerResponse editDeviceSelective(Equipment equipment){

        return equipmentService.updateByPrimaryKeySelective(equipment);
    }
    @DeleteMapping("deleteEquipmentById/{id}")
    @ResponseBody
    public ServerResponse deleteEquipmentById(@PathVariable Long id){

        return  equipmentService.deleteByPrimaryKey(id);
    }

    @PostMapping("deleteEquipmentByIds")
    @ResponseBody
    public ServerResponse deleteEquipmentByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return equipmentService.deleteMany(array);
    }


}
